package com.ns.scs.ads.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ns.core.annotations.ReqLog;
import com.ns.core.bo.IdsBO;
import com.ns.core.sentinel.LimitType;
import com.ns.core.sentinel.RateLimiter;
import com.ns.core.utils.sg.ResultOk;
import com.ns.scs.ads.bo.AdsEntryBO;
import com.ns.scs.ads.bo.AdsPageBO;
import com.ns.scs.ads.dto.AdsPageDto;
import com.ns.scs.ads.entry.AdsEntry;
import com.ns.scs.ads.service.IAdsService;
import com.ns.scs.ads.vo.AdsExtraVO;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

/**
 * 广告相关操作
 *
 * @author lw
 * @since 2024-11-15
 */
@RestController
@RequestMapping("/ads")
@RequiredArgsConstructor
@CrossOrigin
public class AdsController {

    private final IAdsService service;

    /**
     * 改
     *
     * @param bo 配置
     */
    @PostMapping("add")
    @ReqLog(module = "广告模块", name = "增")
    public ResultOk add(@RequestBody AdsEntryBO bo) {
        AdsEntry entry = new AdsEntry();
        BeanUtils.copyProperties(bo, entry);
        entry.setSort(service.getMaxSort() + 1);
        return ResultOk.getByBoolean(service.save(entry));
    }

    /**
     * 改
     *
     * @param bo 配置
     */
    @PostMapping("update")
    @ReqLog(module = "广告模块", name = "改")
    public ResultOk update(@RequestBody AdsEntryBO bo) {
        AdsEntry entry = new AdsEntry();
        BeanUtils.copyProperties(bo, entry);
        return ResultOk.getByBoolean(service.updateById(entry));
    }

    /**
     * 逻辑删
     *
     * @param id 参数
     */
    @GetMapping("delete")
    @ReqLog(module = "广告模块", name = "逻辑删")
    ResultOk delete(Long id) {
        return ResultOk.getByBoolean(service.removeById(id));
    }

    /**
     * 删 - 根据id列表
     */
    @PostMapping("deleteByIds")
    @ReqLog(module = "广告模块", name = "批量删")
    public ResultOk deleteByIds(@RequestBody IdsBO bo) {
        return ResultOk.getByBoolean(service.removeBatchByIds(bo.getIds()));
    }

    /**
     * 查询详情
     *
     * @param id 参数
     */
    @GetMapping("getById")
    @ReqLog(module = "广告模块", name = "详情")
    public ResultOk get(Long id) {
        AdsEntry entry = service.getById(id);
        AdsExtraVO vo = new AdsExtraVO();
        BeanUtils.copyProperties(entry, vo);
        return ResultOk.getSuccessData(vo);
    }

    /**
     * 查-集合
     *
     * @param bo 参数
     */
    @PostMapping("getList")
    @ReqLog(module = "广告模块", name = "查-集合")
    @RateLimiter(time = 5, count = 15, limitType = LimitType.IP)
    public ResultOk getList(@RequestBody AdsPageBO bo) {
        AdsPageDto dto = new AdsPageDto();
        BeanUtils.copyProperties(bo, dto);
        Page<AdsExtraVO> page = service.getList(dto);
        return ResultOk.getPageData(page.getTotal(), page.getRecords());
    }

    /**
     * 查询广告
     *
     * @param code 服务编码
     */
    @GetMapping("getBySCode")
    @ReqLog(module = "广告模块", name = "服务广告", authFlag = false)
    @RateLimiter(time = 5, count = 15, limitType = LimitType.IP)
    public ResultOk getByServerCode(String code) {
        return ResultOk.getSuccessData(service.getByServerCode(code));
    }
}
